package com.kxf.androidtestdemo.activity;

import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.kxf.androidtestdemo.R;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolActivity extends AppCompatActivity {

    private ThreadPoolExecutor threadPoolExecutor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thread_pool);

        threadPoolExecutor = new ThreadPoolExecutor(3,5,4, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(1000));
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i = 0;i<150;i++){
                    final int finali = i;
                    Runnable runnable = new Runnable() {
                        int id = finali;
                        @Override
                        public void run() {
                            int sum = 0;
                            while (sum<200){
                                sum++;
                                try {
                                    Log.d("Thread", "线程已经运行: "+id + " " + Thread.currentThread().getName());
                                    Thread.sleep(500);
                                    Log.d("Thread", "run: "+id);
                                    Log.d("当前线程：",Thread.currentThread().getName());
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    };
                    threadPoolExecutor.execute(runnable);

//                    try {
//                        Thread.sleep(4000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
                    Log.d("添加线程：",finali + "");
                }
                threadPoolExecutor.shutdownNow();
                while (true){
                    Log.d("线程getPoolSize：",threadPoolExecutor.getPoolSize() + "");
                    Log.d("线程getPoolSize：","isTerminating()= " + threadPoolExecutor.isTerminating());
                    try {
                        Thread.sleep(100);
                        if (threadPoolExecutor.isTerminated()){
                            //判断任务是否执行完成
                            Log.d("线程getPoolSize：","threadPoolExecutor.isTerminated()= " + threadPoolExecutor.isTerminated());
                            Log.d("线程getPoolSize：",threadPoolExecutor.getPoolSize() + "");
                            break;
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        }).start();
    }
}
